package com.wyp168.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyp168.commonutils.R;
import com.wyp168.serviceedu.entity.EduSubject;
import com.wyp168.serviceedu.entity.excel.SubjectData;
import com.wyp168.serviceedu.entity.subjecet.OneLevelSubject;
import com.wyp168.serviceedu.entity.subjecet.TwoLevelSubject;
import com.wyp168.serviceedu.listener.SubjectExcelListener;
import com.wyp168.serviceedu.mapper.EduSubjectMapper;
import com.wyp168.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wyp168
 * @since 2022-05-03
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneLevelSubject> getAllOneTwoLevelSubject() {
        //1. 查询所有的一级分类
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapper);
        //2. 查询所有的二级分类
        QueryWrapper<EduSubject> queryWrapper2 = new QueryWrapper<>();
        queryWrapper.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapper2);

        //将结果按照前端所能接收的格式封装到该集合中
        List<OneLevelSubject> resultSubjectList = new ArrayList<>();
        //3. 封装一级分类
        //查询出来的所有的一级分类list集合遍历，得到每一个一级分类对象
        //然后将一级分类对象的`id`属性和`title`属性封装到ResultSubje ctList集合中
        for(int i=0;i<oneSubjectList.size();i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneLevelSubject oneLevelSubject = new OneLevelSubject();
            BeanUtils.copyProperties(eduSubject,oneLevelSubject);

            //封装同一个一级分类的二级分类`id`和`name`
            List<TwoLevelSubject> sameOneLevelSubjectTwoLevel = new ArrayList<>();
            for (EduSubject twoSubject : twoSubjectList) {
                //判断二级分类的parent_id是否等于以及分类的id
                if(twoSubject.getParentId().equals(oneLevelSubject.getId())) {
                    TwoLevelSubject twoLevelSubject = new TwoLevelSubject();
                    twoLevelSubject.setId(twoSubject.getId());
                    twoLevelSubject.setTitle(twoSubject.getTitle());
                    sameOneLevelSubjectTwoLevel.add(twoLevelSubject);
                }
            }
            //将封装过的 相同的一级分类的二级分类集合 放到一级分类对象中
            oneLevelSubject.setChildren(sameOneLevelSubjectTwoLevel);
            resultSubjectList.add(oneLevelSubject);

        }

        //4. 封装二级分类

        return resultSubjectList;
    }
}
